﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DomainInfoLib
{
    /// <summary>
    /// 查询组合
    /// </summary>
    public class QueryGroup
    {
        private List<string> _Domains = new List<string>();

        private List<string> _KeyWords = new List<string>();

        private List<string> _SurFixes = new List<string>();

        private List<string> _PreFixes = new List<string>();



        private bool _DoQueryKeyWord = true;

        private bool _DoQueryPreFixesAndKeyWord = false;

        private bool _DoQueryKeyWordsAndSurFixes = false;

        private bool _DoQueryPreFixesAndKeyWordsAndSurFixes = false;

        public int Count 
        {
            get 
            {
                return _Items.Count;
            }
        }

        private List<QueryItem> _Items = new List<QueryItem>();

        public List<QueryItem> Items
        {
            get 
            {
                return _Items;
            }
        }

        public void AddDomains(List<string> domains)
        {
            _Domains = domains;
        }

        public void AddKeyWords(List<string> keywords)
        {
            _KeyWords = keywords;
        }

        public void AddSurFixes(List<string> surFixes)
        {
            _SurFixes = surFixes;
        }

        public void AddPreFixes(List<string> preFixes)
        {
            _PreFixes = preFixes;
        }

        public void DoQueryKeyWord(bool doQueryKeyWord)
        {
            _DoQueryKeyWord = doQueryKeyWord;
        }

        public void DoQueryPreFixesAndKeyWord(bool doQueryPreFixesAndKeyWord)
        {
            _DoQueryPreFixesAndKeyWord = doQueryPreFixesAndKeyWord;
        }

        public void DoQueryKeyWordsAndSurFixes(bool doQueryKeyWordsAndSurFixes)
        {
            _DoQueryKeyWordsAndSurFixes = doQueryKeyWordsAndSurFixes;
        }

        public void DoQueryPreFixesAndKeyWordsAndSurFixes(bool doQueryPreFixesAndKeyWordsAndSurFixes)
        {
            _DoQueryPreFixesAndKeyWordsAndSurFixes = doQueryPreFixesAndKeyWordsAndSurFixes;
        }

        public void BuildItems()
        {
            _Items = new List<QueryItem>();

            QueryItem _item=new QueryItem();

            foreach (string keyword in _KeyWords)
            {

                foreach (string domain in _Domains)
                {
                    QueryItem item = new QueryItem();

                    item.KeyWord = keyword;

                    item.Domain = domain;

                    if (_DoQueryKeyWord)
                    {
                        _Items.Add(item);
                    }


                    if (_DoQueryKeyWordsAndSurFixes)
                    {
                        _Items.AddRange(GetKeyAndSurItems(keyword,domain));
                    }


                    if (_DoQueryPreFixesAndKeyWord)
                    {
                        _Items.AddRange(GetPreFixesAndKeyWordItems(keyword,domain));
                    }


                    if (_DoQueryPreFixesAndKeyWordsAndSurFixes)
                    {
                        _Items.AddRange(GetPreFixesAndKeyWordsAndSurFixesItems(keyword,domain));
                    }
                }
            }

           
        }

        private List<QueryItem> GetPreFixesAndKeyWordsAndSurFixesItems(string keyword, string domain)
        {
            List<QueryItem> items = new List<QueryItem>();

            foreach (string preFix in _PreFixes)
            {
                



                foreach (string surFix in _SurFixes)
                {
                    QueryItem item = new QueryItem();
                    item.KeyWord = keyword;
                    item.Domain = domain;
                    item.PreFix = preFix;
                    item.SurFix = surFix;
                    items.Add(item);
                }
            }

            return items;
        }

        private List<QueryItem> GetPreFixesAndKeyWordItems(string keyword, string domain)
        {
            List<QueryItem> items = new List<QueryItem>();

            foreach (string preFix in _PreFixes)
            {
                QueryItem item = new QueryItem();

                item.KeyWord = keyword;
                item.Domain = domain;
                item.PreFix = preFix;
                item.SurFix = "";
                items.Add(item);
            }

            return items;
        }

        private List<QueryItem> GetKeyAndSurItems(string keyword, string domain)
        {
            List<QueryItem> items = new List<QueryItem>();

            foreach (string surFix in _SurFixes)
            {
                QueryItem item = new QueryItem();

                item.KeyWord = keyword;
                item.Domain = domain;
                item.PreFix = "";
                item.SurFix = surFix;
                items.Add(item);
            }

            return items;
        }



        public IList<string> GetStrList()
        {
            List<string> list = new List<string>();

            foreach (QueryItem item in Items)
            {
                list.Add(item.DomainFullName);
            }

            return list;
        }
    }
}
